package concurrent;

import java.util.Random;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author Summerday
 */
public class LinkedTransferQueueTest {

    public static void main (String[] args) throws InterruptedException {

        LinkedTransferQueue<Integer> queue = new LinkedTransferQueue<>();

        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                try {
                    int product = queue.take();
                    System.out.println(Thread.currentThread().getName() + " 消费产品id " + product);
                    System.out.println("---------------------------------------------");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "consumer - id - " + i).start();
        }

        for (int i = 1; i <= 10; i++) {
            TimeUnit.MILLISECONDS.sleep(100);
            new Thread(() -> {
                int product = new Random().nextInt(500);
                System.out.println(Thread.currentThread().getName() + " 生产产品id " + product);
                queue.put(product);
            }, "producer - id - " + i).start();
        }
    }

}
